package com.ece.pfe_rover_pvt_ines_ece_application.helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

public class CsvFileHelper_ {

	/**
	 * Retrieve the CSV file path
	 * @param fileName
	 * @return dossierPath
	 */
    public static String getResourcePath(String fileName) {
    	File sdRep = Environment.getExternalStorageDirectory();
		File f = new File(sdRep + "/" + fileName);
		if(!f.exists()){
			throw new RuntimeException("Fichier innexistant sur la carte SD");
		}
       final String dossierPath = f.getAbsolutePath() + File.separator + fileName;
       return dossierPath;
   }

    /**
     * Retrieve the CSV file to read
     * @param fileName
     * @return file
     */
   public static File getResource(String fileName) {
       final String completeFileName = getResourcePath(fileName);
       File file = new File(completeFileName);
       return file;
   }
   
   
   /**
    * Read the CSV file content
    * @param file
    * @return CSV file results as string values
 * @throws IOException 
    */
   public static List<String> readFile(File file) throws IOException {
	   int id = 0;
       List<String> result = new ArrayList<String>();

       FileReader fr = new FileReader(file);
       BufferedReader br = new BufferedReader(fr);

       for (String line = br.readLine(); line != null; line = br.readLine()) {    	   
    	   if(line.startsWith("#")){
				continue;
			}
    	   line = line.concat(String.valueOf(id)+";"+"\n");    	   
    	   result.add(line);    	    
    	   System.out.println(line);
    	   id++;
       }

       br.close();
       fr.close();

       return result;
   }
}
